package com.liming.system.controller.admin;

import com.liming.server.dto.PageDto;
import com.liming.server.dto.ResourceDto;
import com.liming.server.dto.ResponseDto;
import com.liming.server.service.ResourceService;
import com.liming.server.util.ValidatorUtil;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/admin/resource")
public class ResourceController {
    public static final String BUSINESS_NAME="资源";

    @Resource
    private ResourceService resourceService;

    @PostMapping("/list")
    public ResponseDto list(@RequestBody PageDto pageDto){
        ResponseDto responseDto = new ResponseDto();
        resourceService.list(pageDto);
        responseDto.setContent(pageDto);
        return responseDto;
    }
    @PostMapping("/save")
    public ResponseDto save(@RequestBody String jsonStr){

        //保存校验
        ValidatorUtil.require(jsonStr,"资源");

        ResponseDto responseDto = new ResponseDto();
        resourceService.saveJson(jsonStr);
        return responseDto;
    }

    @DeleteMapping("/delete/{id}")
    public ResponseDto delete(@PathVariable String id){
        ResponseDto responseDto = new ResponseDto();
        resourceService.delete(id);
        return responseDto;
    }

    /**
     * 资源树的查询
     * @return
     */
    @GetMapping("/load-tree")
    public ResponseDto loadTree(){
        ResponseDto responseDto = new ResponseDto();
        List<ResourceDto> responseDtoList=resourceService.loadTree();
        responseDto.setContent(responseDtoList);
        return responseDto;
    }
}
